//
//  ST_RefuseLoveCell.swift
//  btvideo
//
//  Created by JarvanZhang on 2020/2/5.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit
import SwiftyJSON

class ST_RefuseLoveCell: UITableViewCell {

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = .none
        
        contentView.addSubview(icon_image_view)
        icon_image_view.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview().offset(15)
            make.bottom.equalToSuperview().offset(-15)
            make.width.equalTo(icon_image_view.snp.height)
        }
        
        contentView.addSubview(user_name_label)
        user_name_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(21)
            make.left.equalTo(icon_image_view.snp.right).offset(10)
            if ScreenWidth < 400{
                make.width.lessThanOrEqualTo(145)
            }   
        }
        
        contentView.addSubview(distance_label)
        distance_label.snp.makeConstraints { (make) in
            make.bottom.equalTo(icon_image_view).offset(-2)
            make.left.equalTo(icon_image_view.snp.right).offset(10)
        }
        
        contentView.addSubview(re_acceipt_btn)
        re_acceipt_btn.snp.makeConstraints { (make) in
            make.width.equalTo(90)
            make.height.equalTo(30)
            make.top.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
        }
        
        contentView.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.height.equalTo(1)
            make.right.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(15)
        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
//    MARK:- properties
    fileprivate lazy var icon_image_view:UIImageView = {
       let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = 25
        return control
    }()
    
    fileprivate lazy var user_name_label:UILabel = {
       let control = UILabel()
        control.textColor = UIColor.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var distance_label:UILabel = {
       let control = UILabel()
        control.textColor = UIColor.init(hexString: "#ACB4C0")
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    fileprivate lazy var re_acceipt_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
       control.setTitleForAllStates("重新喜欢")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        control.backgroundColor = Color.init(hexString: "#FEC207")
        control.setTitleColorForAllStates(UIColor.white)
        control.cornerRadius = 15
        control.add_touch_up(insideTarget: self, action: #selector(acceipt_handle))
        return control
    }()
    
    fileprivate lazy var line:UIView = {
       let control = UIView()
        control.backgroundColor = Color.init(hexString: "#F2F4F7")
        return control
    }()
    
    fileprivate var model = JSON.null
    
//    var acceipt_closure:((JSON)->())?
    
    
    var removeClosure:((JSON)->())?
}
//MARK: - bind_model
extension ST_RefuseLoveCell{
    
    func bind_model(json:JSON){
        
        model = json
        
        
        icon_image_view.pin_setImage(from: json["avatar"].stringValue.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        user_name_label.text = json["nickname"].stringValue
        
        distance_label.text = json["address"].stringValue + " " + json["distance"].stringValue
    }
}

//MARK: - Event
extension ST_RefuseLoveCell{
    
    @objc func acceipt_handle(){
        
        PlayFeedBack()
     
        self.re_acceipt_btn.isUserInteractionEnabled = false
         
        let param = ["target_accid":model["accid"].stringValue]
 
        JVrequest(PH.relikeActionV50Api,parameters: param, success: { (json) in
            
//            NotificationCenter.default.post(name: Notification.Name.showedSomeOne, object: nil, userInfo: ["fromController":fromVC ?? 0,"id":model["accid"].stringValue,"isThumbUp":true])
            
            
            if json["data"]["mutual_like"].boolValue{
                                    
                ST_BTUserInfoManager.shared.receive_match(json: json["data"])
                
                let info = ST_MatchUserInfo.init(accid: self.model["accid"].stringValue, avatar: self.model["avatar"].stringValue, nickname: self.model["nickname"].stringValue)
                
                ST_BTBootController.shared.matchSuccess(info: info)
                
            }
            
            self.removeClosure?(self.model)
            
//            self.acceipt_closure?(self.model)
            
            self.re_acceipt_btn.isUserInteractionEnabled = true
            
        }) { (code, err) in
            
            self.re_acceipt_btn.isUserInteractionEnabled = true
            
            self.parentViewController?.toast(text: err)
        }
        
    }
}


//MARK: - 拒绝的看过

class ST_RefuseLookCell: UITableViewCell {

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = .none
        
        contentView.addSubview(icon_image_view)
        icon_image_view.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview().offset(15)
            make.bottom.equalToSuperview().offset(-15)
            make.width.equalTo(icon_image_view.snp.height)
        }
        
        contentView.addSubview(user_name_label)
        user_name_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(21)
            make.left.equalTo(icon_image_view.snp.right).offset(10)
            if ScreenWidth < 400{
                make.width.lessThanOrEqualTo(145)
            }
        }
        
        contentView.addSubview(distance_label)
        distance_label.snp.makeConstraints { (make) in
            make.bottom.equalTo(icon_image_view).offset(-2)
            make.left.equalTo(icon_image_view.snp.right).offset(10)
        }
        
        contentView.addSubview(re_acceipt_btn)
        re_acceipt_btn.snp.makeConstraints { (make) in
            make.width.equalTo(90)
            make.height.equalTo(30)
            make.top.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
        }
        
        contentView.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.height.equalTo(1)
            make.right.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(15)
        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
//    MARK:- properties
    fileprivate lazy var icon_image_view:UIImageView = {
       let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = 25
        return control
    }()
    
    fileprivate lazy var user_name_label:UILabel = {
       let control = UILabel()
        control.textColor = UIColor.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var distance_label:UILabel = {
       let control = UILabel()
        control.textColor = UIColor.init(hexString: "#ACB4C0")
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    fileprivate lazy var re_acceipt_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
       control.setTitleForAllStates("查看")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        control.backgroundColor = Color.init(hexString: "#FEC207")
        control.setTitleColorForAllStates(UIColor.white)
        control.cornerRadius = 15
        control.add_touch_up(insideTarget: self, action: #selector(acceipt_handle))
        return control
    }()
    
    fileprivate lazy var line:UIView = {
       let control = UIView()
        control.backgroundColor = Color.init(hexString: "#F2F4F7")
        return control
    }()
    
    fileprivate var model = JSON.null
    
    var acceipt_closure:((JSON)->())?
}
//MARK: - bind_model
extension ST_RefuseLookCell{
    
    func bind_model(json:JSON){
        
        model = json
        
        
        icon_image_view.pin_setImage(from: json["avatar"].stringValue.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        user_name_label.text = json["nickname"].stringValue
        
        distance_label.text = json["address"].stringValue + " " + json["distance"].stringValue
    }
}

//MARK: - Event
extension ST_RefuseLookCell{
    
    @objc func acceipt_handle(){
        
        let vc = ST_PairsCenterViewController()
        
        vc.accid = model["accid"].stringValue

        let nav = PairsCenterInteractiveNavigationController.init(rootViewController: vc)
        
        nav.overCurrentContext = true
        
        GlobalPresent(toController: nav)
        
    }
}
